package com.sx.zongzhi.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.sx.zongzhi.R;
import com.sx.zongzhi.activity.ImagePlayActivity;
import com.sx.zongzhi.bean.PopulationDetailBean;
import com.sx.zongzhi.net.ServiceFactory;
import com.sx.zongzhi.utils.BaseUtil;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by XZY on 2017/11/28.
 * 基本信息
 */
public class BasicInforFragment extends Fragment {

    @BindView(R.id.imv_Head) ImageView imv_Head;
    @BindView(R.id.et_IDCard) TextView tv_IDCard;
    @BindView(R.id.et_Name) TextView tv_Name;
    @BindView(R.id.et_Sex) TextView tv_Sex;
    @BindView(R.id.tv_Birthday) TextView tv_Birthday;
    @BindView(R.id.tv_ResidenceType) TextView tv_ResidenceType;
    @BindView(R.id.et_Nation) TextView tv_Nation;
    @BindView(R.id.et_PoliticalStatus) TextView tv_PoliticalStatus;
    @BindView(R.id.tv_Marriage) TextView tv_Marriage;
    @BindView(R.id.et_HouseHoldPlace) TextView tv_HouseHoldPlace;
    @BindView(R.id.et_BirthPlace) TextView tv_BirthPlace;
    @BindView(R.id.et_NowAddress) TextView tv_NowAddress;
    @BindView(R.id.et_HouseholdAddress) TextView tv_HouseholdAddress;
    @BindView(R.id.tv_NowAddYesNo) TextView tv_NowAddYesNo;
    @BindView(R.id.tv_TitleNowAdd) TextView tv_TitleNowAdd;
    @BindView(R.id.et_NowHouseAddress) TextView tv_NowHouseAddress;
    @BindView(R.id.et_OldName) TextView tv_OldName;
    @BindView(R.id.et_ContactWay) TextView tv_ContactWay;
    @BindView(R.id.et_Education) TextView tv_Education;
    @BindView(R.id.et_WorkType) TextView tv_WorkType;
    @BindView(R.id.et_Profession) TextView tv_Profession;
    @BindView(R.id.et_Religion) TextView tv_Religion;
    @BindView(R.id.et_Service) TextView tv_Service;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_basic_infor,container,false);
        ButterKnife.bind(this,view);
        initData();
        return view;
    }


    private void initData() {
        final PopulationDetailBean detailBean= (PopulationDetailBean) getArguments().getSerializable("PopulationDetailBean");
        if (detailBean!=null){
            initView(detailBean);
        }


    }



    private void initView(PopulationDetailBean detailBean) {
        tv_IDCard.setText(detailBean.getIdCardNo());
        tv_Name.setText(detailBean.getName());
        tv_Sex.setText(detailBean.getGender()!=null? detailBean.getGender().getDisplayName():"未知");
        tv_Birthday.setText(detailBean.getBirthday());
        tv_ResidenceType.setText(detailBean.getResidenceType()!=null ?
                detailBean.getResidenceType().getDisplayName():"未知");
        tv_Nation.setText(detailBean.getNation()!=null?detailBean.getNation().getDisplayName():"未知");
        tv_PoliticalStatus.setText(detailBean.getPoliticalBackground()!=null ?
                detailBean.getPoliticalBackground().getDisplayName():"未知");
        tv_Marriage.setText(detailBean.getMaritalState()!=null?detailBean.getMaritalState().getDisplayName():"未知");
        tv_HouseHoldPlace.setText(detailBean.getDomicilePlace());
        tv_BirthPlace.setText(detailBean.getPlace());
        tv_NowAddress.setText(detailBean.getPresent());
        tv_HouseholdAddress.setText(detailBean.getNativePlaceAddress());
        if (detailBean.isIsHaveHouse()){
            tv_NowAddYesNo.setText("有");
            tv_TitleNowAdd.setText("现住门(楼)详址");
            tv_NowHouseAddress.setText(detailBean.getCurrentAddress());
        }else {
            tv_NowAddYesNo.setText("无");
            tv_TitleNowAdd.setText("无住所原因");
            tv_NowHouseAddress.setText(detailBean.getNoHouseReason());
        }
        tv_OldName.setText(detailBean.getUsedName());
        String contact= (BaseUtil.isEmpty(detailBean.getMobileNumber())?"":detailBean.getMobileNumber())
                +" "+(BaseUtil.isEmpty(detailBean.getTelephone())?"":detailBean.getTelephone());
        tv_ContactWay.setText(contact);
        tv_Education.setText(detailBean.getSchoolStr());
        tv_WorkType.setText(detailBean.getCareer()!=null? detailBean.getCareer().getDisplayName():"未知");
        tv_Profession.setText(detailBean.getCareerText());
        tv_Religion.setText(detailBean.getFaith()!=null? detailBean.getFaith().getDisplayName():"未知");
        tv_Service.setText(detailBean.getWorkUnit());
        if (!BaseUtil.isEmpty(detailBean.getImgUrl())){
            final String path=ServiceFactory.NEW_API_BASE_URL+ File.separator+detailBean.getImgUrl();
            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.mipmap.head_default)
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
            Glide.with(getActivity())
                    .load(path)
                    .apply(options)
                    .into(imv_Head);
            imv_Head.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(getActivity(), ImagePlayActivity.class)
                            .putExtra("path",path));
                }
            });
        }
    }
}
